#include "SysMenu.h"
//#include "GameControlMenu.h"
#include "MyMain.h"
#include "AboutLayer.h"
#include "SettingsLayer.h"
#include "SimpleAudioEngine.h"
using namespace cocos2d;
SysMenu::SysMenu(){}
bool SysMenu::init() {
	if ( !CCLayer::init() )
	{
		return false;
	}
	//winSize = CCSizeMake(320,480);
	this->setKeypadEnabled(true);
	/*cocos2d::CCSprite* sp1 = CCSprite::create("loading.png");
	sp1->setAnchorPoint(cocos2d::CCPointZero);
	this->addChild(sp1, 0, 1);*/
    CCSprite *t2= CCSprite::create("fengmian.png");
	//t1->setAnchorPoint( ccp(0, 0));
	t2->setPosition(ccp(210,500));
    //t2->setScaleX(0.7);
    addChild(t2);
    CCSprite *t1 = CCSprite::createWithSpriteFrameName("battlecity.png");
	//t1->setAnchorPoint( ccp(0, 0));
	t1->setPosition(ccp(210,320));
    t1->setScaleX(0.7);
    addChild(t1);

		/*CCSprite* logo = CCSprite::createWithSpriteFrameName("icon.png");
	logo->setAnchorPoint(cocos2d::CCPointZero);
	logo->setPosition(ccp(100, 310));
	this->addChild(logo, 10, 1);*/

	CCSprite* newGameNormal = CCSprite::createWithSpriteFrameName("menu_0_0.png");
	CCSprite *newGameSelected = CCSprite::createWithSpriteFrameName("menu_1_0.png"		);
	CCSprite *newGameDisabled = CCSprite::createWithSpriteFrameName("menu_2_0.png");

	CCSprite* gameSettingsNormal = CCSprite::createWithSpriteFrameName("menu_0_1.png");
	CCSprite* gameSettingsSelected = CCSprite::createWithSpriteFrameName("menu_1_1.png");
	CCSprite* gameSettingsDisabled = CCSprite::createWithSpriteFrameName("menu_2_1.png");

	CCSprite *aboutNormal = CCSprite::createWithSpriteFrameName("menu_0_2.png");
	CCSprite *aboutSelected = CCSprite::createWithSpriteFrameName("menu_1_2.png");
	CCSprite *aboutDisabled = CCSprite::createWithSpriteFrameName("menu_2_2.png");

	CCMenuItemSprite* newGame = CCMenuItemSprite::create(newGameNormal,
		newGameSelected, newGameDisabled, this, menu_selector(SysMenu::functionNewGame));
	CCMenuItemSprite *gameSettings = CCMenuItemSprite::create(
		gameSettingsNormal, gameSettingsSelected, gameSettingsDisabled,
		this, menu_selector(SysMenu::onSettings));
	CCMenuItemSprite *about = CCMenuItemSprite::create(aboutNormal,
		aboutSelected, aboutDisabled, this, menu_selector(SysMenu::onAbout));

	CCMenu *menu = CCMenu::create(newGame, gameSettings, about,NULL);
	//menu->alignItemsVertically();
	menu->alignItemsVerticallyWithPadding(25);
	this->addChild(menu, 1, 2);
	menu->setPosition(ccp(global_winSize.width / 2,
		global_winSize.height / 4));
	this->schedule(schedule_selector(SysMenu::update), 3.0f);

	//CCTexture2D* tmp = CCTextureCache::sharedTextureCache()
	//	->addImage("ship01.png");
	//CCSpriteFrame * tmpf=CCSpriteFrame::createWithTexture(tmp,CCRectMake(0, 45, 60, 38));
	//this->_ship = CCSprite::createWithSpriteFrame(tmpf);
	//this->addChild(this->_ship, 0, 4);
	//this->_ship->setPosition(ccp(global_winSize.width*rand()/(RAND_MAX+1.0), 10));
	//this->_ship->runAction(CCMoveBy::create(
	//	2,
	//	ccp(global_winSize.width*rand()/(RAND_MAX+1.0),
	//	this->_ship->getPosition().y + global_winSize.height)));


	if (global_sound) {
		//CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(s_mainMainMusic,	 true);

		//cc.AudioManager.sharedEngine().setBackgroundMusicVolume(0.7);
		//cc.AudioManager.sharedEngine().playBackgroundMusic(Resource.s_mainMainMusic,	 true); 
	}
	return true;
}

void SysMenu::removeIt()
{
	//removeChild(flare,true);
}
void SysMenu::functionNewGame(CCObject* pSender) {
	onButtonEffect();
	//flareEffect();
	onNewGame();
}
//
void SysMenu::onNewGame() {
	/*if(global_difficult)
	{
		//EnemyWave0.ShowTime=2;
		/EnemyWave0.ShowTime=8;
	}
	else
	{
		EnemyWave0.ShowTime=4;
		EnemyWave0.ShowTime=16;

	}*/
	CCScene *scene = CCScene::create();
	scene->addChild(MyMain::create());
	//scene->addChild(GameControlMenu::create());
	CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.2f, scene));
}
//void SysMenu::onButtonEffect(){
//	if (global_sound) {
//		//var s = cc.AudioManager.sharedEngine().playEffect(s_buttonEffect);
//		CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(s_buttonEffect);
//	}
//}
void SysMenu::onSettings(CCObject *pSender) {
	onButtonEffect();
	CCScene *scene = CCScene::create();
	scene->addChild(SettingsLayer::create());
	CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.2,
	 scene));
}

void SysMenu::onAbout(CCObject* pSender) {
	//this.onButtonEffect();
	CCScene* scene = CCScene::create();
	scene->addChild(AboutLayer::create());
	CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.2,
	scene));
}
//
void SysMenu::update(float dt) {
	/*if (this->_ship->getPosition().y > 480) {
		this->_ship->setPosition(ccp( global_winSize.width*rand()/(RAND_MAX+1.0), 10));
		this->_ship->runAction(CCMoveBy::create((int) (1+4.0f*rand()/(RAND_MAX+1.0) ),
			ccp((float) ( global_winSize.width*rand()/(RAND_MAX+1.0)),
			this->_ship->getPosition().y + 480)));
	}*/
	/* if (this._ship.getPosition().y > 480) {
            this._ship.setPosition(cc.ccp(Math.random() * winSize.width, 10));
            this._ship.runAction(cc.MoveBy.actionWithDuration(parseInt(5 * Math.random()), cc.ccp(Math.random() * winSize.width, this._ship.getPosition().y + 480)));
        }*/
}

	 void SysMenu::onButtonEffect() {
		if (global_sound) {
			//todo SoundEngine.playEffect(Resource.s_buttonEffect);
			//CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(s_buttonEffect);
		}
	}


CCScene *SysMenu::scene() {
	CCScene* scene = CCScene::create();
	CCLayer* layer = SysMenu::create();
	scene->addChild(layer);
	return scene;
}
void SysMenu::keyBackClicked(){

	CCLog("Android- KeyBackClicked!");
	CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
}